<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        
        *{
            margin:0;
            padding: 0;
        }
     
        #menu{
            position: absolute;
            width:204px;
            border:1px solid #d2d2d2;
            display:none;
        }
        li{
            list-style:none;
            line-height: 36px;
            width:200px;
            text-align: center;
            color:#333;
            border-bottom: 1px solid #d2d2d2;
            cursor: pointer;
        }
        li:nth-of-type(2n) {
            background: #f2f2f2;
        }
    </style>
</head>
<body>
    <ul id="menu">
        <li>刷新</li>
        <li>音乐</li>
        <li>美食</li>
        <li>图书</li>
    </ul>
    <script>
        /**
         *  加上 区域限制范围
         * **/
        var menu = document.querySelector('#menu');
        document.addEventListener('contextmenu',function(e){
            e.preventDefault();
            var scrollT = document.body.scrollTop || document.documentElement.scrollTop ;
            var l = e.pageX  - 204;
            var t = e.pageY  - 150  + scrollT;

            if( l > e.pageX ) {
                l = l
            }
            if( l < 0 ) {
                l = 0
            }
            if(  t > e.pageY ) {
                t = t;
            }
            if( t < 0 ) {
                t = 0
            }
            css(menu,{
                left : l + 'px',
                top:  t + 'px' ,
                display : 'block'
            })
        });
        document.addEventListener('click',function(e){
        
            css( menu,{
                display: 'none'
            });
        })


        function css( obj,sVal ) {
            if( typeof sVal == 'object' &&  sVal ) {
                for( var i in sVal ) {
                    obj.style[i] = sVal[i];
                }
            }
            return 0;
        }
    </script>
</body>
</html>